﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Log4NetToCore3.Models;
using NLog;

namespace Log4NetToCore3.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        //构造函数的注入
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;

            try
            {
                throw new Exception("fddswa");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex,"错误信息。因某些故障停止工作");
            }

            _logger.Log(Microsoft.Extensions.Logging.LogLevel.Information, new EventId(100, ""),
              $"Start processing HTTP request");
            Logger log = LogManager.GetCurrentClassLogger();
            LogEventInfo theEvent = new LogEventInfo(NLog.LogLevel.Debug, "", "Pass my custom value");
            theEvent.Properties["Userid"] = "My custom string";
            log.Log(theEvent);
            #region 测试日志
            _logger.LogTrace("开发阶段调试，可能包含敏感程序数据",new Content() { Userid ="dewqgtfrwe"});
            _logger.LogDebug("开发阶段短期内比较有用，对调试有益。", "3473456789056789");
            _logger.LogInformation("你访问了首页。跟踪程序的一般流程。");
            _logger.LogWarning("警告信息！因程序出现故障或其他不会导致程序停止的流程异常或意外事件。");
            _logger.LogError("错误信息。因某些故障停止工作");
            _logger.LogCritical("程序或系统崩溃、遇到灾难性故障！！！");
            #endregion
        }

        public IActionResult Index()
        {
            _logger.LogDebug("This is HomeController_Index");
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
